package com.example.springbootdemo.controller;

import com.example.springbootdemo.service.pdfFillService;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * pdfFillController
 *
 * @date: 2025/7/21 18:55
 * @version: 1.0
 */
@RestController
@RequestMapping("/pdfFill")
@CrossOrigin
@Slf4j
public class pdfFillController {

    @Resource
    private pdfFillService pdfFillService;
    @PostMapping("/generate")
    public void generateReport(@RequestParam("formData") String formDataJson, @RequestParam("testPicture_af_image") MultipartFile imageDataJson, HttpServletResponse response) throws IOException {

        // 解析formData，JSON字符串转对象
        ObjectMapper mapper = new ObjectMapper();      // ObjectMapper用于在 Java 对象和 JSON 数据之间进行转换
        Map<String, String> formData = mapper.readValue(formDataJson, Map.class);
        // 构建图片数据，因为图片数据是二进制数据，所以需要将MultipartFile转换为byte[]  思路2：不直接使用二进制数据进行传输，而是携带图片url集合，然后从OSS中获取
        Map<String, byte[]> imageData = new HashMap<>();
        imageData.put("testPicture_af_image", imageDataJson.getBytes());

        pdfFillService.fillPdf(formData, imageData, response);
    }
}
